home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / xlisp2.arc / XLSTR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-01-01  |  4.4 KB  |  206 lines

  1. /* xlstr - xlisp string builtin functions */
  2.  
  3. #include "xlisp.h"
  4.  
  5. /* external variables */
  6. extern NODE *xlstack;
  7.  
  8. /* external procedures */
  9. extern char *strcat();
  10.  
  11. /* xstrlen - length of a string */
  12. NODE *xstrlen(args)
  13.   NODE *args;
  14. {
  15.     NODE *val;
  16.     int total;
  17.  
  18.     /* initialize */
  19.     total = 0;
  20.  
  21.     /* loop over args and total */
  22.     while (args != NULL)
  23.     total += strlen(xlmatch(STR,&args)->n_str);
  24.  
  25.     /* create the value node */
  26.     val = newnode(INT);
  27.     val->n_int = total;
  28.  
  29.     /* return the total */
  30.     return (val);
  31. }
  32.  
  33. /* xstrcat - concatenate a bunch of strings */
  34. NODE *xstrcat(args)
  35.   NODE *args;
  36. {
  37.     NODE *oldstk,val,*p;
  38.     char *str;
  39.     int len;
  40.  
  41.     /* create a new stack frame */
  42.     oldstk = xlsave(&val,NULL);
  43.  
  44.     /* find the length of the new string */
  45.     for (p = args, len = 0; p; )
  46.     len += strlen(xlmatch(STR,&p)->n_str);
  47.  
  48.     /* create the result string */
  49.     val.n_ptr = newnode(STR);
  50.     val.n_ptr->n_str = str = stralloc(len);
  51.     *str = 0;
  52.  
  53.     /* combine the strings */
  54.     while (args)
  55.     strcat(str,xlmatch(STR,&args)->n_str);
  56.  
  57.     /* restore the previous stack frame */
  58.     xlstack = oldstk;
  59.  
  60.     /* return the new string */
  61.     return (val.n_ptr);
  62. }
  63.  
  64. /* xsubstr - return a substring */
  65. NODE *xsubstr(args)
  66.   NODE *args;
  67. {
  68.     NODE *oldstk,arg,src,val;
  69.     int start,forlen,srclen;
  70.     char *srcptr,*dstptr;
  71.  
  72.     /* create a new stack frame */
  73.     oldstk = xlsave(&arg,&src,&val,NULL);
  74.  
  75.     /* initialize */
  76.     arg.n_ptr = args;
  77.     
  78.     /* get string and its length */
  79.     src.n_ptr = xlmatch(STR,&arg.n_ptr);
  80.     srcptr = src.n_ptr->n_str;
  81.     srclen = strlen(srcptr);
  82.  
  83.     /* get starting pos -- must be present */
  84.     start = xlmatch(INT,&arg.n_ptr)->n_int;
  85.  
  86.     /* get length -- if not present use remainder of string */
  87.     if (arg.n_ptr != NULL)
  88.     forlen = xlmatch(INT,&arg.n_ptr)->n_int;
  89.     else
  90.     forlen = srclen;        /* use len and fix below */
  91.  
  92.     /* make sure there aren't any more arguments */
  93.     xllastarg(arg.n_ptr);
  94.  
  95.     /* don't take more than exists */
  96.     if (start + forlen > srclen)
  97.     forlen = srclen - start + 1;
  98.  
  99.     /* if start beyond string -- return null string */
  100.     if (start > srclen) {
  101.     start = 1;
  102.     forlen = 0; }
  103.     
  104.     /* create return node */
  105.     val.n_ptr = newnode(STR);
  106.     val.n_ptr->n_str = dstptr = stralloc(forlen);
  107.  
  108.     /* move string */
  109.     for (srcptr += start-1; forlen--; *dstptr++ = *srcptr++)
  110.     ;
  111.     *dstptr = 0;
  112.  
  113.     /* restore the previous stack frame */
  114.     xlstack = oldstk;
  115.  
  116.     /* return the substring */
  117.     return (val.n_ptr);
  118. }
  119.  
  120. /* xascii - return ascii value */
  121. NODE *xascii(args)
  122.   NODE *args;
  123. {
  124.     NODE *val;
  125.  
  126.     /* build return node */
  127.     val = newnode(INT);
  128.     val->n_int = *(xlmatch(STR,&args)->n_str);
  129.  
  130.     /* make sure there aren't any more arguments */
  131.     xllastarg(args);
  132.  
  133.     /* return the character */
  134.     return (val);
  135. }
  136.  
  137. /* xchr - convert an INT into a one character ascii string */
  138. NODE *xchr(args)
  139.   NODE *args;
  140. {
  141.     NODE *oldstk,val;
  142.     char *sptr;
  143.  
  144.     /* create a new stack frame */
  145.     oldstk = xlsave(&val,NULL);
  146.  
  147.     /* build return node */
  148.     val.n_ptr = newnode(STR);
  149.     val.n_ptr->n_str = sptr = stralloc(1);
  150.     *sptr++ = xlmatch(INT,&args)->n_int;
  151.     *sptr = 0;
  152.  
  153.     /* make sure there aren't any more arguments */
  154.     xllastarg(args);
  155.  
  156.     /* restore the previous stack frame */
  157.     xlstack = oldstk;
  158.  
  159.     /* return the new string */
  160.     return (val.n_ptr);
  161. }
  162.  
  163. /* xatoi - convert an ascii string to an integer */
  164. NODE *xatoi(args)
  165.   NODE *args;
  166. {
  167.     NODE *val;
  168.     int n;
  169.  
  170.     /* get the string and convert it */
  171.     n = atoi(xlmatch(STR,&args)->n_str);
  172.  
  173.     /* make sure there aren't any more arguments */
  174.     xllastarg(args);
  175.  
  176.     /* create the value node */
  177.     val = newnode(INT);
  178.     val->n_int = n;
  179.  
  180.     /* return the number */
  181.     return (val);
  182. }
  183.  
  184. /* xitoa - convert an integer to an ascii string */
  185. NODE *xitoa(args)
  186.   NODE *args;
  187. {
  188.     NODE *val;
  189.     char buf[20];
  190.     int n;
  191.  
  192.     /* get the integer */
  193.     n = xlmatch(INT,&args)->n_int;
  194.     xllastarg(args);
  195.  
  196.     /* convert it to ascii */
  197.     sprintf(buf,"%d",n);
  198.  
  199.     /* create the value node */
  200.     val = newnode(STR);
  201.     val->n_str = strsave(buf);
  202.  
  203.     /* return the string */
  204.     return (val);
  205. }
  206.